﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using DCRF.Common.Interface;
using DCRF.Common.Connection;

namespace DCRF.BaseBlocks.Commander.Console.Nodes
{
    class BlockNode: INode
    {
        public TreeNode myNode = null;
        public IBlockWeb myWeb = null;
        public Guid myId = Guid.Empty;

        public BlockNode(TreeNode node, IBlockWeb web, Guid id)
        {
            myNode = node;
            myWeb = web;
            myId = id;
            myNode.Tag = this;

            Refresh();            
        }

        public object ProcessRequest(string serviceName, params object[] args)
        {
            if (serviceName == "REF")
            {
                Refresh();
            }
            else if (serviceName == "ADD")
            {
                int counter = (int)args[0];
                Connector c = args[1] as Connector;

                c.Raise("Item" + counter.ToString(), myWeb[myId]);
            }
            else if (serviceName == "COP")
            {
                Clipboard.SetData(DataFormats.Text, myId.ToString());
            }

            return null;
        }

        public List<string> Commands
        {
            get
            {
                List<string> result = new List<string>();
                result.Add("REF");
                result.Add("ADD");
                result.Add("COP");

                return result;
            }
        }

        public void Refresh()
        {
            myNode.Nodes.Clear();
            //TODO: rewrite using new attributes
            //myNode.Text = myWeb[myId].BlockInfo.BlockID.ToString() + " (" + myId.ToString() + ")";

            TreeNode tn = new TreeNode();
            myNode.Nodes.Add(tn);
            new ServicesNode(tn, myWeb, myId);

            TreeNode cs = new TreeNode();
            myNode.Nodes.Add(cs);

            new ConnectorsNode(cs, myWeb, myId);

            IBlockWeb innerWeb = myWeb[myId].ProcessRequest("GetInnerWeb") as IBlockWeb;

            if (innerWeb != null)
            {
                TreeNode iw = new TreeNode();
                myNode.Nodes.Add(iw);

                new BlockWebNode(iw, innerWeb);
            }
        }
    }
}
